﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Imaging;

namespace Coproject.Controls
{
	public class ImageButton : Button
	{
		public string ImageName
		{
			get { return (string)GetValue(ImageNameProperty); }
			set { SetValue(ImageNameProperty, value); }
		}

		public static readonly DependencyProperty ImageNameProperty =
			DependencyProperty.Register("ImageName", typeof(string), typeof(ImageButton), new PropertyMetadata(OnImageNamePropertyChanged));

		public static void OnImageNamePropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
		{
			ImageButton button = d as ImageButton;
			string newValue = e.NewValue as string;

			if (newValue.IsNullOrWhiteSpace())
			{
				button.Content = null;
				return;
			}

			newValue = "/Coproject;component/Assets/Icons/{0}.png".FormatWith(newValue);
			ImageSource image = new BitmapImage(new Uri(newValue, UriKind.Relative));
			button.Content = new Image { Source = image };
		}
	}
}
